<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\Adsense;
use App\Models\Article;
use App\Models\Department;
use App\Models\Doctor;
use App\Models\Goods;
use App\Models\GoodsCategory;
use App\Models\Mechanism;
use App\Models\Partner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;


class IndexController extends Controller
{
    // 首页
    public function index()
    {
        return $this->writeJson(0, [
            'adsense' => Adsense::getIndex(), // 顶部轮播图
            'department' => Department::getListAll(), // 科室
            'mechanism_rec' => Mechanism::getIndexRec(), // 推荐机构
            'doctor_rec' => Doctor::getIndexRec(), // 推荐医师
            'partner' => Partner::getIndex(), // 合作伙伴
        ]);
    }

    // 首页 - 科室id获取医师
    public function indexDoctor(Request $request)
    {
        $department_id = $request->post('department_id');
        $list = Doctor::whereDepartmentId($department_id)->limit(3)->get($columns = [
            'department_id', 'department_name', 'avatar',
            'name', 'positions', 'hospital',
            'price', 'visits_num', 'skilled',
            'id'
        ]);
        return $this->writeJson(0, $list);
    }

    //上传图片
    public function upload(Request $request)
    {
        if ($request->hasFile('image') && $request->file('image')->isValid()) {
            $disk = Storage::disk('image');
            $photo = $request->file('image');
            $realPath = $photo->getRealPath();//临时文件路径
            $name = hash_file('md5',$realPath);
            $time = date('Y-m-d');
            $fileName = 'image/'.$time.'/'.$name.'.'.$photo->getClientOriginalExtension();
            $bool = $disk->put($fileName, file_get_contents($realPath));
            // 判断是否上传成功
            if ($bool) {
                return $this->writeJson(0,['path'=>$disk->url($fileName)]);
            }
        }
        return $this->writeJson(1,null,'上传错误');
    }

    // 科室列表
    public function department()
    {
        return $this->writeJson(0, Department::getListAll());
    }

    // 商城首页
    public function shops()
    {
        $categoryList = GoodsCategory::orderByDesc('sorts')
            ->limit(7)
            ->get();

        $goodsRecList = Goods::whereIsRec(1)
            ->where('status', 1)
            ->limit(4)
            ->get(['id', 'name', 'cover', 'price']);

        $goodsLoveList = Goods::whereStatus(1)
            ->orderByDesc('sale_num')
            ->limit(4)
            ->get(['id', 'name', 'cover', 'price']);

        $articleList = Article::whereStatus(1)
            ->limit(3)
            ->get(['id', 'thumb', 'title', 'created_at']);
        foreach ($articleList as $item) {
            $item->createdAt = $item->created_at->format('Y-m-d');
        }

        return $this->writeJson(0, [
            'adsense' => Adsense::getIndexShops(), // 顶部轮播图
            'category' => $categoryList, // 分类
            'goods_rec_list' => $goodsRecList, // 热门推荐商品
            'goods_love_list' => $goodsLoveList, // 大家爱买
            'articleList' => $articleList, // 健康攻略
            'partner' => Partner::getIndex(), // 合作伙伴
        ]);
    }
}
